#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2020/4/20 0020 10:51
# @Author  : liangshuo
# @File    : t_model.py
# @Software : PyCharm

# django脚本化启动

import os, django

# 加载配置文件
env = os.environ.get('ENV')
if env == "prod":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.prod")
elif env == "dev":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.dev")
else:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.local")

# 启动django的方式之一
django.setup()  # 跑socket

from apps.foreignkey.models import Author, AuthorDetail

# 查询数据
author = Author.objects.first()
print(author.name)
print(author.detail.mobile)

detail = AuthorDetail.objects.first()
print(detail.mobile)
print(detail.author.name)

# 1) 作者删除，详情删除---级联删除
# AuthorDetail.objects.filter(pk=1).delete()
# Author.objects.filter(pk=1).delete()
